{% macro initial_cluster() -%}
{% for host in groups[etcd_peers_group] -%}
  {{ hostvars[host]['ansible_hostname'] }}={{ etcd_peer_url_scheme }}://
  {%- if etcd_interface != "" -%}
    {{ hostvars[host]['ansible_' + etcd_interface].ipv4.address }}
  {%- else -%}
    {{ hostvars[host].ansible_default_ipv4.address }}
  {%- endif -%}
:{{ etcd_peer_port }}
  {%- if not loop.last -%},{%- endif -%}
{%- endfor -%}
{% endmacro -%}

{% if groups[etcd_peers_group] and groups[etcd_peers_group] | length > 0 %}
ETCD_NAME={{ ansible_hostname }}
{% else %}
ETCD_NAME=default
{% endif %}
ETCD_DATA_DIR={{ etcd_data_dir }}
#ETCD_SNAPSHOT_COUNTER="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""

{% if groups[etcd_peers_group] and groups[etcd_peers_group] | length > 0 %}
#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS={{ etcd_initial_advertise_peer_urls }}
ETCD_INITIAL_CLUSTER={{ initial_cluster() }}
ETCD_INITIAL_CLUSTER_STATE={{ etcd_initial_cluster_state }}
ETCD_INITIAL_CLUSTER_TOKEN={{ etcd_initial_cluster_token }}
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_SRV=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
{% endif %}

{% if groups[etcd_peers_group] and groups[etcd_peers_group] | length > 0 and networking == "contiv" %}
ETCD_LISTEN_PEER_URLS={{ etcd_listen_peer_legacy_urls }}
{% else %}
ETCD_LISTEN_PEER_URLS={{ etcd_listen_peer_urls }}
{% endif %}

{% if networking == "contiv" %}
ETCD_ADVERTISE_CLIENT_URLS={{ etcd_advertise_client_legacy_urls }}
ETCD_LISTEN_CLIENT_URLS={{ etcd_listen_client_legacy_urls }}
{% else %}
ETCD_ADVERTISE_CLIENT_URLS={{ etcd_advertise_client_urls }}
ETCD_LISTEN_CLIENT_URLS="{{ etcd_listen_client_urls }}"
{% endif %}

#[proxy]
{% if etcd_is_proxy or (networking == "contiv" and inventory_hostname in groups['nodes']) %}
ETCD_PROXY="on"
{% else %}
ETCD_PROXY="off"
{% endif %}

#[security]
{% if etcd_url_scheme == 'https' -%}
ETCD_CA_FILE={{ etcd_ca_file }}
ETCD_TRUSTED_CA_FILE={{ etcd_ca_file }}
ETCD_CERT_FILE={{ etcd_cert_file }}
ETCD_KEY_FILE={{ etcd_key_file }}
ETCD_CLIENT_CERT_AUTH={{ etcd_client_cert_auth }}
{% endif -%}
{% if etcd_peer_url_scheme == 'https' -%}
ETCD_PEER_CA_FILE={{ etcd_peer_ca_file }}
ETCD_PEER_TRUSTED_CA_FILE={{ etcd_peer_ca_file }}
ETCD_PEER_CERT_FILE={{ etcd_peer_cert_file }}
ETCD_PEER_KEY_FILE={{ etcd_peer_key_file }}
ETCD_PEER_CLIENT_CERT_AUTH={{ etcd_peer_client_cert_auth }}
{% endif -%}
